package cn.stylefeng.guns.modular.ums.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.MemberAccount;
import cn.stylefeng.guns.modular.ums.mapper.MemberAccountMapper;
import cn.stylefeng.guns.modular.ums.model.params.MemberAccountParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberAccountResult;
import  cn.stylefeng.guns.modular.ums.service.MemberAccountService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.MediaName;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 会员账户表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2019-11-07
 */
@Service
public class MemberAccountServiceImpl extends ServiceImpl<MemberAccountMapper, MemberAccount> implements MemberAccountService {


    @Override
    public void addMemberBalance(BigDecimal money, Integer memberId) {
        MemberAccount account=this.findByMemberId(memberId);
        account.setBalance(account.getBalance().add(money));
        MemberAccountParam accountParam=new MemberAccountParam();
        ToolUtil.copyProperties(account, accountParam);
        update(accountParam);
    }

    @Override
    public void add(MemberAccountParam param){
        MemberAccount entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MemberAccountParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(MemberAccountParam param){
        MemberAccount oldEntity = getOldEntity(param);
        MemberAccount newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MemberAccountResult findBySpec(MemberAccountParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public MemberAccount findByMemberId(Integer memberId) {
        MemberAccount memberAccount=new MemberAccount();
        memberAccount.setMemberId(memberId);
        QueryWrapper queryWrapper=new QueryWrapper(memberAccount);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<MemberAccountResult> findListBySpec(MemberAccountParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MemberAccountParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MemberAccountParam param){
        return param.getAccountId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MemberAccount getOldEntity(MemberAccountParam param) {
        return this.getById(getKey(param));
    }

    private MemberAccount getEntity(MemberAccountParam param) {
        MemberAccount entity = new MemberAccount();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
